<?php
namespace app\shop\model;

use app\BaseModel;
use think\facade\Db;

class Printer extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_shop_printer';



    protected $append = [

        'store_id'

    ];


    /**
     * @author chenniang
     * @DataTime: 2021-10-08 16:34
     * @功能说明:绑定的门店
     */
    public function getStoreIdAttr($value,$data){

        if(!empty($data['id'])){

            $store_id =  Db::name('longbing_card_shop_printer_store')->where(['printer_id'=>$data['id']])->column('store_id');

            return array_values($store_id);
        }

    }

    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        if(isset($data['store_id'])){

            $store_id = $data['store_id'];

            unset($data['store_id']);
        }

        $res = $this->insert($data);

        if(!empty($store_id)){

            $id = $this->getLastInsID();

            $this->updateSome($id,$store_id,$data['uniacid']);
        }


        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        if(isset($data['store_id'])){

            $store_id = $data['store_id'];

            unset($data['store_id']);
        }


        $res = $this->where($dis)->update($data);


        if(!empty($store_id)){

            $this->updateSome($data['id'],$store_id,$data['uniacid']);
        }

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page){

        $data = $this->where($dis)->order('id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2021-10-08 16:11
     * @功能说明:绑定门店
     */
    public function updateSome($id,$data,$uniacid){


        Db::name('longbing_card_shop_printer_store')->where(['printer_id'=>$id])->delete();

        if(!empty($data)){

            foreach ($data as $k=>$v){

                $insert = [

                    'uniacid'    => $uniacid,

                    'printer_id' => $id,

                    'store_id'   => $v

                ];

                Db::name('longbing_card_shop_printer_store')->insert($insert);

            }

        }

        return true;


    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-11 09:25
     * @功能说明:订单打印
     */
    public function userOrder($id){

        $order_model   = new AdminShopOrder();
        //订单信息
        $order   = $order_model->orderInfo(['id'=>$id]);

        if($order['address_id']==-1){

            $order_type = '自提订单';

        }else{

            $order_type = '物流订单';

        }

        $brs = "<BR>";

        $orderInfo = '<CB>'.$order_type.'</CB><BR>';

        $orderInfo .= '订单号:' . $order['out_trade_no'].$brs;

        $orderInfo .= ''.$brs;

        $orderInfo .= '下单时间:'.date('Y-m-d H:i:s',$order['create_time']).$brs;

        $orderInfo .= ''.$brs;

        if($order['address_id']!=-1){

            $orderInfo .= '服务员:'.$order['staff_text'].$brs;

            $orderInfo .= ''.$brs;

        }

        if($order['address_id']==-1&&!empty($order['zt_time'])){

            $zt_time = explode('-',$order['zt_time']);

            foreach ($zt_time as &$value){

                $value = date('Y-m-d H:i:s',$value);
            }

            $orderInfo .= '自提时间:'.implode('-',$zt_time).$brs;

            $orderInfo .= ''.$brs;

        }

        $orderInfo .= '--------------------------------'.$brs;

        $orderInfo .= '商品/规格/数量/价格'.$brs;

        $orderInfo .= '--------------------------------'.$brs;

        $num = 0;


        foreach ($order['goods_text'] as $v) {

            $num += $v['number'];

            $v['name'] = mb_convert_encoding($v['name'], "UTF-8", "auto");

            $orderInfo.= $v['name'].'     '.$v['content'].'    x'.$v['number'].'    '.round($v['price'],2).'元'.$brs;

            $orderInfo .= ''.$brs;

            $orderInfo .= ''.$brs;

        }

//      dump($orderInfo);exit;

        $orderInfo .= '--------------------------------'.$brs;

        $orderInfo .= ''.$brs;

        $orderInfo .= '支付金额:' . $order['total_price'] . '元'.$brs;

        if(!empty($order['coupon_pay'])){

            $orderInfo .= ''.$brs;

            $orderInfo .= '优惠券抵扣:' . $order['coupon_pay'] . '元'.$brs;
        }

        if(!empty($order['freight'])){

            $orderInfo .= ''.$brs;

            $orderInfo .= '配送费:' . $order['freight'] . '元'.$brs;
        }

        $orderInfo .= ''.$brs;

        $orderInfo .= '用户信息:' . $order['address_text'].$brs;

        $orderInfo .= ''.$brs;

        if(!empty($order['remarks'])){

            $orderInfo .= '备注:' . $order['remarks'].$brs;
        }

        return $orderInfo;

    }




    /**
     * @author chenniang
     * @DataTime: 2020-11-11 10:20
     * @功能说明:
     */
    public function storeOrderPrinter($printer_config,$orderInfo){

        $printer_config = $this->where('id','in',$printer_config)->where(['status'=>1])->select()->toArray();

        if(!empty($printer_config)){

            foreach ($printer_config as $value){

                $value['ukey'] = $value['api_key'];

                $value['sn']   = $value['printer_key'];

//                if($value['user_ticket']==1){
                    //用户小票
                    $res = \longbingcore\printer\Printer::FeiePrintMsg($value,$orderInfo,'Open_printMsg',$value['user_ticket_num']);

//                    dump($res);exit;
//                }

            }

        }

        return true;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-12 09:57
     * @功能说明:打印
     */
    public function printer($id,$aotu=1){
        //用户小票
        $user_order  = $this->userOrder($id);

        $order_model = new AdminShopOrder();

        $order   = $order_model->orderInfo(['id'=>$id]);

        $printer = $this->getOrderPrinter($order['store_id'],$order['uniacid'],$aotu);
        //打印
        $res     = $this->storeOrderPrinter($printer,$user_order);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-10-08 17:08
     * @功能说明:获取订单对应的打印机
     */
    public function getOrderPrinter($store_id,$uniacid,$auto){

        $dis = [

            'a.status'  => 1,

            'b.store_id'=> $store_id,

            'a.self'    => 1,

            'a.auto'    => $auto
        ];

        $store_data = $this->alias('a')
                ->join('longbing_card_shop_printer_store b','a.id = b.printer_id')
                ->where($dis)
                ->column('a.id');

        $dis = [

            'status' => 1,

            'uniacid'=> $uniacid,

            'self'   => 0,

            'auto'   => $auto
        ];

        $all_data = $this->where($dis)->column('id');

        $all_data = array_merge($all_data,$store_data);

//        dump($all_data);exit;

        return $all_data;
    }








}